package com.terrier.ui.controls.supportClasses
{
    import com.terrier.ui.controls.WxButton;
    import com.terrier.ui.events.WxButtonEvent;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.IWxTrackBaseSkin;

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    /**
     * 滑块式组件基类. 这是滚动条和滑块条的基类.
     * @author Jarvis.weng
     */
    public class WxTrackBase extends WxRange
    {
        /**
         * @private
         */
        protected var track:WxButton;

        /**
         * @private
         */
        protected var thumb:WxButton;

        /**
         * @private
         */
        protected var isDrag:Boolean = false;

        private var _clickOffset:Point;

        /**
         * 构造函数
         */
        public function WxTrackBase()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            super.setSkin(value);

            if (skin != null)
            {
                track.setSkin(skin.trackSkin);
                thumb.setSkin(skin.thumbSkin);
            }
            else
            {
                track.setSkin(null);
                thumb.setSkin(null);
            }

            invalidateView();
			invalidateLayout();
        }

        /**
         * @private
         */
        public override function set value(newValue:Number):void
        {
            var oldValue:Number = value;
            super.value = newValue;
            if (oldValue != value)
            {
                if (!isDrag)
                {
                    updateThumbPosition();
                }
            }
        }

        /**
         * 获取或者设置一个布尔值, 指示组件是否响应用户鼠标操作.
         * @return
         */
        public function get enabled():Boolean
        {
            return contains(thumb);
        }

        /**
         * @private
         */
        public function set enabled(value:Boolean):void
        {
            if (value)
            {
                addChild(thumb);
            }
            else if (contains(thumb))
            {
                removeChild(thumb);
            }
            track.enabled = value;
        }

        /**
         * @private
         */
        override protected function constrainProperties(minimumChanged:Boolean,
                                                        maximumChanged:Boolean,
                                                        stepSizeChanged:Boolean):void
        {
            super.constrainProperties(minimumChanged, maximumChanged, stepSizeChanged);
			
//			updateLayout();
        }

        /**
         * @private
         */
        protected function pointToValue(pt:Point):Number
        {
            // subclass override this method for custom behavior
            return minimum;
        }

        /**
         * @private
         */
        protected function updateThumbPosition():void
        {
            // subclass override this method for custom behavior
        }

        /**
         * @private
         */
        protected function dragThumb(pt:Point):void
        {
            // subclass override this method for custom behavior
        }

        /**
         * @private
         */
        protected function startDragThumb(pt:Point):void
        {
            // subclass override this method for custom behavior
        }

        /**
         * @private
         */
        protected function stopDragThumb(pt:Point):void
        {
            // subclass override this method for custom behavior
        }

        /**
         * @private
         */
        override protected function initialize():void
        {
			super.initialize();
			
            if (track == null)
            {
                track = new WxButton();
                addChild(track);
            }

            if (thumb == null)
            {
                thumb = new WxButton();
                addChild(thumb);

                thumb.addEventListener(WxButtonEvent.BUTTON_PRESS, thumbPressHandler);
            }
        }

        //--------------------------------------------------
        //
        // Event Listeners
        //
        //--------------------------------------------------

        private function thumbPressHandler(e:WxButtonEvent):void
        {
            thumb.addEventListener(WxButtonEvent.BUTTON_RELEASE, thumbReleaseHandler);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);

            _clickOffset = new Point(thumb.mouseX, thumb.mouseY);

            isDrag = true;
            startDragThumb(thumbOrigin);
        }

        private function thumbReleaseHandler(e:WxButtonEvent):void
        {
            thumb.removeEventListener(WxButtonEvent.BUTTON_RELEASE, thumbReleaseHandler);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);

            isDrag = false;
            stopDragThumb(thumbOrigin);
        }

        private function stageMouseMoveHandler(e:MouseEvent):void
        {
            dragThumb(thumbOrigin);
            e.updateAfterEvent();
        }

        private function get thumbOrigin():Point
        {
            return new Point(mouseX - _clickOffset.x, mouseY - _clickOffset.y);
        }

        private function get skin():IWxTrackBaseSkin
        {
            return _skin as IWxTrackBaseSkin;
        }
    }
}
